﻿Imports System
Imports System.Collections
Imports System.Globalization
Imports System.Workflow.ComponentModel.Compiler
Imports System.Workflow.ComponentModel.Design

Namespace System.Workflow.ComponentModel
    Friend NotInheritable Class FaultHandlersActivityValidator
        Inherits CompositeActivityValidator
        ' Methods
        Public Overrides Function Validate(ByVal manager As ValidationManager, ByVal obj As Object) As ValidationErrorCollection
            Dim errors As ValidationErrorCollection = MyBase.Validate(manager, obj)
            Dim activity As FaultHandlersActivity = [TryCast](obj,FaultHandlersActivity)
            If (activity Is Nothing) Then
                Throw New ArgumentException(SR.GetString("Error_UnexpectedArgumentType", New Object() { GetType(FaultHandlersActivity).FullName }), "obj")
            End If
            Dim hashtable As New Hashtable
            Dim list As New ArrayList
            Dim flag As Boolean = False
            Dim activity2 As Activity
            For Each activity2 In activity.EnabledActivities
                If TypeOf activity2 Is FaultHandlerActivity Then
                    Dim activity3 As FaultHandlerActivity = DirectCast(activity2, FaultHandlerActivity)
                    Dim faultType As Type = activity3.FaultType
                    If (Not faultType Is Nothing) Then
                        If (hashtable.Item(faultType) Is Nothing) Then
                            hashtable.Item(faultType) = 1
                            list.Add(faultType)
                        ElseIf (CInt(hashtable.Item(faultType)) = 1) Then
                            errors.Add(New ValidationError(String.Format(CultureInfo.CurrentCulture, SR.GetString("Error_ScopeDuplicateFaultHandlerActivityFor"), New Object() { Helpers.GetEnclosingActivity(activity).GetType.Name, faultType.FullName }), &H520))
                            hashtable.Item(faultType) = 2
                        End If
                        Dim type2 As Type
                        For Each type2 In list
                            If ((Not type2 Is faultType) AndAlso type2.IsAssignableFrom(faultType)) Then
                                errors.Add(New ValidationError(SR.GetString("Error_FaultHandlerActivityWrongOrder", New Object() { faultType.Name, type2.Name }), &H521))
                            End If
                        Next
                    End If
                ElseIf Not flag Then
                    errors.Add(New ValidationError(SR.GetString("Error_FaultHandlersActivityDeclNotAllFaultHandlerActivityDecl"), &H51E))
                    flag = True
                End If
            Next
            If (activity.AlternateFlowActivities.Count > 0) Then
                errors.Add(New ValidationError(SR.GetString("Error_ModelingConstructsCanNotContainModelingConstructs"), &H61F))
            End If
            Return errors
        End Function

    End Class
End Namespace

